package main

import "fmt"

/**
 * 数据容器
 */
func main() {
	//array()
	mapContainer()
}

// 数组和切片
func array() {
	var a_array [20]int
	var b_array [2]string = [2]string{"111", "222"}
	c_array := [3]string{"222", "333", "444"}
	fmt.Println(a_array)
	fmt.Println(b_array)
	fmt.Println(c_array)
	//切片不用声明大小
	//声明切片
	var a_slice []string

	a_slice = append(a_slice, "2")
	a_slice = append(a_slice, "3")
	a_slice = append(a_slice, "4")
	a_slice = append(a_slice, "5")
	a_slice = append(a_slice, "6")
	fmt.Println(a_slice)

	//创建切片
	b_slice := make([]int, 10)
	b_slice[0] = 20
	fmt.Println(b_slice)

	//遍历数组
	for index, val := range a_slice {
		fmt.Println(index, val)
	}
}

// map容器
func mapContainer() {
	var a_map map[string]int
	a_map = make(map[string]int)
	var b_map = make(map[string]string)

	a_map["a"] = 1
	a_map["b"] = 2

	b_map["a"] = "hello"
	b_map["b"] = "world"
	fmt.Println(a_map, b_map)

	//遍历map
	for key, val := range a_map {
		fmt.Println(key, val)
	}

	//map取值

	val, ok := a_map["a"]
	if ok {
		fmt.Println("has value ", val)
	} else {
		fmt.Println("no value")
	}

	val_b, ok_b := b_map["c"]
	if ok_b {
		fmt.Println("has value ", val_b)
	} else {
		fmt.Println("no value")
	}
}
